package com.ele.client.activity.auth;

import com.ele.client.ClientFactory;
import com.ele.client.rpc.SimpleAsyncCallback;
import com.ele.model.AppUser;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class RegistrationActivity extends AbstractActivity implements RegistrationView.Presenter {

    private ClientFactory clientFactory;
    private String email;
    private RegistrationView view;

    public RegistrationActivity(RegistrationPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
        this.email = place.getEmail();
    }

    @Override
    public void start(AcceptsOneWidget container, EventBus eventBus) {
        view = clientFactory.getRegistrationView();
        view.setEmail(email);
        view.setPresenter(this);
        container.setWidget(view.asWidget());
    }

    @Override
    public void register(String email, String password, String conformPassword) {
        view.startLoading();

        clientFactory.getApplicationService().signUp(email, password, new SimpleAsyncCallback<AppUser>() {
            @Override
            public void onSuccess(AppUser user) {
//                PathHelper.setAction("home");
//                History.fireCurrentHistoryState();
            }

            @Override
            public void onFailure(Throwable caught) {
                super.onFailure(caught);
                view.stopLoading();
            }
        });
    }
}
